/*
 * ReportJPanel.java
 *
 * Created on Oct 3, 2010, 9:19:10 PM
 */
package ws.performancetest.app.gui.panel;

import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import ws.utils.collections.table.Column;
import ws.utils.collections.table.ITable;
import ws.utils.collections.table.Table;
import ws.performancetest.app.gui.DBConnectionListener;
import ws.performancetest.app.gui.dialog.ErrorDialog;
import ws.performancetest.app.gui.strings.AppStrings;
import ws.utils.jdbc.DBManager;

/**
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class ReportJPanel extends javax.swing.JPanel {

  ITable table = new Table();
  private TableModel resultsTableDataModel = new AbstractTableModel() {

    public int getRowCount() {
      return table.getRowCount();
    }

    public int getColumnCount() {
      return table.getColumnCount();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
      return table.getRowList().get(rowIndex).getColumn(columnIndex);
    }
  };

  /** Creates new form ReportJPanel */
  public ReportJPanel() {
    initComponents();

    try {
      DBConnectionListener.addComponent(btRefresh);
    } catch (SQLException ex) {
      ErrorDialog.run(AppStrings.createExceptionMessage(ex));
      ex.printStackTrace();
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    btRefresh = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    tblResultTable = new javax.swing.JTable();

    btRefresh.setText("Refresh");
    btRefresh.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btRefreshActionPerformed(evt);
      }
    });

    tblResultTable.setModel(resultsTableDataModel);
    jScrollPane1.setViewportView(tblResultTable);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
          .addComponent(btRefresh))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(btRefresh)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents

  private void btRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRefreshActionPerformed
    try {
      DBManager dbmanager = new DBManager();
      table = dbmanager.getTableContents("VResultSummary");

      tblResultTable = new JTable();
      tblResultTable.setModel(resultsTableDataModel);
      for (int i = 0; i < table.getColumnCount(); ++i) {
        Column tcolumn = table.getHeader().getColumn(i);
        tblResultTable.getTableHeader().getColumnModel().getColumn(i).setHeaderValue(tcolumn.getName());
      }
      jScrollPane1.setViewportView(tblResultTable);
    } catch (SQLException ex) {
      ErrorDialog.run(AppStrings.createExceptionMessage(ex));
      ex.printStackTrace();
    }
  }//GEN-LAST:event_btRefreshActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btRefresh;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable tblResultTable;
  // End of variables declaration//GEN-END:variables
}
